package com.web.base.auth.provider;

import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.cache.RoleMenuAuthCache;
import com.web.base.auth.cache.RoleMenuButtonCache;
import com.web.base.auth.exception.MenuException;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.message.MenuRequest;
import com.web.base.auth.message.MenuResponse;
import com.web.base.auth.message.RoleMenuRequest;
import com.web.base.auth.model.Menu;
import com.web.base.auth.model.MenuAuth;
import com.web.base.auth.model.Role;
import com.web.base.auth.service.MenuAuthService;
import com.web.base.auth.service.MenuService;
import com.web.base.auth.service.RoleService;
import com.web.base.auth.util.MenuConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("authMenuProvider")
public class MenuProvider implements MenuFacade
{
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuAuthService menuAuthService;
	@Autowired
	private RoleMenuAuthCache roleMenuAuthCache;
	@Autowired
	private RoleMenuButtonCache roleMenuButtonCache;
	
	public MenuProvider()
	{
	}

	@Override
	public MenuResponse add(MenuRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Menu menu = MenuConvertor.toMenu(request);
		menu = menuService.save(menu);
		MenuResponse response = MenuConvertor.toResponse(menu, null);
		return response;
	}

	@Override
	public MenuResponse update(MenuRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Menu menu = MenuConvertor.toMenu(request);
		menu = menuService.save(menu);
		MenuResponse response = MenuConvertor.toResponse(menu, null);
		return response;
	}

	@Override
	public MenuResponse get(Integer id)
	{
		if (id == null)
			throw new MenuException("菜单ID不能为空");
		
		Menu menu = menuService.get(id);
		MenuResponse response = MenuConvertor.toResponse(menu, null);
		return response;
	}

	@Override
	public Map<String, List<MenuResponse>> index(Integer userId)
	{
		if (userId == null)
			throw new MenuException("用户ID不能为空");
			
		Map<String, List<Menu>> map = menuService.index(userId);
		Map<String, List<MenuResponse>> responseMap = MenuConvertor.toResponse(map, null);
		return responseMap;
	}

	@Override
	public Map<String, List<MenuResponse>> list(MenuRequest request)
	{
		Menu menu = MenuConvertor.toMenu(request);
		Map<String, List<Menu>> map = menuService.list(menu);
		Map<String, List<MenuResponse>> responseMap = MenuConvertor.toResponse(map, null);
		return responseMap;
	}

	@Override
	public void refresh()
	{
		roleMenuAuthCache.delete(null);
		roleMenuButtonCache.delete(null);
	}

	@Override
	public void addRoleMenu(RoleMenuRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		menuService.addRoleMenu(request.getRoleId(), request.getMenuIds(), request.getAuthIds(), request.getButtonIds());
		roleMenuAuthCache.delete(request.getRoleId());
		roleMenuButtonCache.delete(request.getRoleId());
	}

	@Override
	public Map<String, List<MenuResponse>> listSelect(Integer roleId)
	{
		if (roleId == null)
			throw new MenuException("角色ID不能为空");
		
		Role role = roleService.get(roleId);
		if (role == null)
			return null;
		
		Map<String, List<Menu>> map = menuService.listSelect(roleId, role.getAppId());
		List<MenuAuth> authList = menuAuthService.listSelect(roleId, role.getAppId());
		Map<String, List<MenuResponse>> responseMap = MenuConvertor.toResponse(map, authList);
		return responseMap;
	}

	@Override
	public void delete(Integer id)
	{
		if (id == null)
			throw new MenuException("菜单ID不能为空");

		menuService.delete(id);
	}

}
